import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
// 引入svg
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
// 引入mock
import { viteMockServe } from "vite-plugin-mock";

// https://vitejs.dev/config/
// @ts-ignore
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), "");

  return {
    server: {
      host: "0.0.0.0",
      port: env.VITE_PORT,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE, //实际请求地址
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, "")
        }
        // "/api": {
        //   target: "http://127.0.0.1:1234",
        //   changeOrigin: true,
        //   rewrite: path => path.replace(/^\/api/, "")
        // }
      }
    },

    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
        symbolId: "icon-[dir]-[name]"
      }),
      viteMockServe({
        localEnabled: command === "serve"
      })
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src")
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "@/styles/var.scss";'
        }
      }
    }
  };
});
